const app = getApp();
Page({
  data: {
    searchKey: "",
    activityList: [],
    allStatus: 3, //保存开始或暂停状态
    pageIndex: 1, // 分页数
    loadingStatus: false, // 是否正在加载
    loadText: "点击加载更多",
    msgText: "暂无拼团活动"
  },
  // 搜索输入框的值
  inp_cont(e) {
    let that = this;
    let key = e.detail.value;
    that.setData({
      searchKey: key
    })
    if (key == "") {
      wx.showLoading({
        title: '重新加载列表',
      })
      that.setData({
        pageIndex: 1,
        loadText: "点击加载更多",
        msgText: '暂无拼团活动'
      })
      wx.request({
        url: app.buildUrl('/shop/active/active'),
        header: app.buildHeader(),
        method: "POST",
        data: {
          name: key,
          page: that.data.pageIndex
        },
        success(res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            that.setData({
              activityList: res.data.data[0]
            })
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    }
  },
  // 搜索
  search() {
    let that = this;
    let key = that.data.searchKey;
    if (key == "" || !key) {
      app.showToast('请输入搜索内容')
    } else {
      wx.showLoading({
        title: '搜索中...',
      })
      that.setData({
        pageIndex: 1,
        loadText: "点击加载更多"
      })
      wx.request({
        url: app.buildUrl('/shop/active/active'),
        header: app.buildHeader(),
        method: "POST",
        data: {
          name: key,
          page: that.data.pageIndex
        },
        success(res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            if (res.data.data[0].length == 0) {
              that.setData({
                msgText: '没有搜索到相关内容'
              })
            }
            that.setData({
              activityList: res.data.data[0]
            })
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    }
  },
  // 添加拼团跳转
  append: function() {
    wx.navigateTo({
      url: '/pages/add/add',
    })
  },
  //   全部开始 && 全部暂停
  allToggle(e) {
    let that = this;
    let num = e.target.dataset.num;
    let newList = that.data.activityList;
    let url = num == 1 ? app.buildUrl('/shop/active/begin-all') : app.buildUrl('/shop/active/end-all');
    if (that.data.activityList.length == 0) {
      app.showToast("暂无拼团活动");
      return;
    }
    wx.showLoading({
      title: '正在请求操作...',
    })
    that.setData({
      allStatus: num
    })
    setTimeout(() => {
      that.setData({
        allStatus: 3
      })
    }, 200)
    wx.request({
      url: url,
      header: app.buildHeader(),
      method: "POST",
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          app.showToast(res.data.msg)
          newList.forEach(i => {
            if (num == 1) {
              if (i.status == '0') {
                i.status = '1'
              }
            } else if (num == 2) {
              if (i.status == '1') {
                i.status = '0'
              }
            }
          })
          that.setData({
            activityList: newList
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // 单个暂停 && 单个开始
  stateToggle(e) {
    let that = this;
    let goodId = e.target.dataset.goodId;
    let newList = that.data.activityList;
    if (e.target.dataset.status == 2) {
      app.showToast('活动已过期')
      return;
    } else if (e.target.dataset.status == 3) {
      app.showToast('活动已禁用')
      return;
    }
    wx.showLoading({
      title: '正在请求操作...',
    })
    newList.forEach(i => {
      if (i.id == goodId) {
        //i.status == 1为开始状态
        let url = i.status == '1' ? app.buildUrl('/shop/active/end') : app.buildUrl('/shop/active/begin');
        wx.request({
          url: url,
          header: app.buildHeader(),
          data: {
            id: goodId
          },
          method: "POST",
          success(res) {
            wx.hideLoading();
            if (res.data.code == 200) {
              app.showToast(i.active_name + res.data.msg);
              i.status == '1' ? i.status = '0' : i.status = '1';
              that.setData({
                activityList: newList
              })
            } else {
              app.showToast(res.data.msg)
            }
          }
        })
      }
    })
  },
  // 编辑
  editItem(e) {
    wx.navigateTo({
      url: '../detail/detail?id=' + e.target.dataset.goodsId + '&status=' + e.target.dataset.status,
    })
  },
  // 加载更多
  load() {
    let that = this;
    // that.data.loadingStatus == true 说明还在加载状态
    if (that.data.loadingStatus) {
      return;
    }
    that.setData({
      pageIndex: ++that.data.pageIndex,
      loadText: "正在加载...",
      loadingStatus: true
    })
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.buildUrl('/shop/active/active'),
      header: app.buildHeader(),
      method: "POST",
      data: {
        name: that.data.searchKey,
        page: that.data.pageIndex
      },
      success(res) {
        wx.hideLoading();
        that.setData({
          loadText: "点击加载更多",
          loadingStatus: false
        })
        if (res.data.code == 200) {
          let arr = res.data.data[0];
          if (arr.length == 0) {
            app.showToast('没有更多了');
            that.setData({
              pageIndex: --that.data.pageIndex,
              loadText: "没有更多了"
            })
          } else {
            that.setData({
              activityList: that.data.activityList.concat(arr)
            })
          }
        } else {
          app.showToast(res.data.msg);
          that.setData({
            pageIndex: --that.data.pageIndex
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  onShow(){
    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.buildUrl('/shop/active/active'),
      header: app.buildHeader(),
      method: "POST",
      data: {
        page: that.data.pageIndex
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            activityList: res.data.data[0]
          })
        } else {
          app.showToast(res.data.msg);
        }
      }
    })
  }
})